<?php
namespace Library\Router;

class Router
{
	use \Library\Traits\Patterns\Singleton;
	private function __construct()
	{
	}
	
	public static function dispatchPage($url)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Router_Router)
			var_dump("namespace Library\Router; class Router -> public static function dispatchPage($url)");
		$controllers = self::getControllerClassName("index");
		$action = self::getActionName("index");
		$url = explode("/", $url);
		if (!empty($url[0]))
		{
			if (file_exists(self::getControllerPath($url[0])) && class_exists(self::getControllerClassName($url[0])))
			{
				$controllers = self::getControllerClassName($url[0]);
				array_splice($url, 0, 1);
			}
			else
			{
				$controllers = self::getControllerClassName("error");
			}
		}
		$controllers = new $controllers;
		if (!empty($url[0]))
		{
			if(method_exists($controllers, self::getActionName($url[0])))
				$action = self::getActionName($url[0]);
			array_splice($url, 0, 1);
		}
		call_user_func_array(array($controllers, $action), $url);
		call_user_func_array(array($controllers, "renderView"), array(	"controller" 	=> get_class($controllers),
																		"action" 		=> $action));
		unset($controllers, $action);
	}
	
	public static function dispatchModule($module, $action, $param=array())
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Router_Router)
			var_dump("namespace Library\Router; class Router -> public static function dispatchModule($module, $action, \$param=array())");
		if (file_exists(self::getModulePath($module)) && class_exists(self::getModuleClassName($module)))
		{
			$module = self::getModuleClassName($module);
			$module = new $module;
			if (method_exists($module, self::getActionName($action)))
			{
				$action = self::getActionName($action);
				call_user_func_array(array($module, $action), $param);
				call_user_func_array(array($module, "renderModule"), array(	"module" 	=> get_class($module),
																			"action" 	=> $action));
			}
		}
	}
	
	private static function getModulePath($name)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Router_Router)
			var_dump("namespace Library\Router; class Router -> private static function getModulePath($name)");
		return APP_ROOT."Modules".DIRECTORY_SEPARATOR.ucfirst(strtolower("$name")).".php";
	}
	
	private static function getModuleClassName($name)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Router_Router)
			var_dump("namespace Library\Router; class Router -> private static function getModuleClassName($name)");
		return "\Application\Modules\\".ucfirst(strtolower($name));
	}
	
	private static function getControllerPath($name)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Router_Router)
			var_dump("namespace Library\Router; class Router -> private static function getControllerPath($name)");
		return APP_ROOT."Controllers".DIRECTORY_SEPARATOR.ucfirst(strtolower("$name")).".php";
	}
	
	private static function getControllerClassName($name)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Router_Router)
			var_dump("namespace Library\Router; class Router -> private static function getControllerClassName($name)");
		return "\Application\Controllers\\".ucfirst(strtolower($name));
	}
	
	private static function getActionName($name)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Router_Router)
			var_dump("namespace Library\Router; class Router -> private static function getActionName($name)");
		return strtolower($name)."Action";
	}
}
?>